package com.ht.module.sys.controller;

import com.ht.config.log.annotation.SysLog;
import com.ht.constant.DicConstant;
import com.ht.module.sys.entity.SysMenu;
import com.ht.module.sys.service.SysMenuService;
import com.ht.util.JsonResult;
import com.ht.util.Pager;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author Auto-generator
 * @since 2021-05-13
 */
@RestController
@RequestMapping("/sys-menu")
@Tag(name = "菜单 ")
public class SysMenuController {


    @Autowired
    private SysMenuService sysMenuService;


    @GetMapping("/findMenuRole/{rid}")
    @Operation(summary = "按角色查询出菜单")
    @SysLog(msg = "按角色查询出菜单")
    public JsonResult findMenuRole(@PathVariable(value = "rid") String rid) {
        return JsonResult.success(sysMenuService.findMenuRole(rid));
    }


    @GetMapping("/findMenuTree")
    @Operation(summary = "查询出菜单树")
    @PreAuthorize("hasAnyAuthority('SYS_MENU')")
    @SysLog(msg = "查询出菜单树")
    public JsonResult findMenuTree() {
        return JsonResult.success(sysMenuService.findMenuTree());
    }


    @GetMapping("/findPage")
    @Operation(summary = "分页查询")
    @PreAuthorize("hasAnyAuthority('SYS_MENU')")
    @SysLog(msg = "分页查询菜单树")
    public JsonResult findPage(Pager<SysMenu> pager, SysMenu sysMenu) {
        return JsonResult.success(sysMenuService.page(pager));
    }


    @PostMapping("/add")
    @Operation(summary = "新增")
    @PreAuthorize("hasAnyAuthority('SYS_MENU_ADD')")
    @SysLog(msg = "新增菜单", type = DicConstant.OperLogType.ADD)
    public JsonResult add(@RequestBody @Validated SysMenu sysMenu) {
        sysMenuService.add(sysMenu);
        return JsonResult.success();
    }


    @PutMapping("/update")
    @Operation(summary = "修改")
    @PreAuthorize("hasAnyAuthority('SYS_MENU_UPDATE')")
    @SysLog(msg = "修改菜单", type = DicConstant.OperLogType.UPDATE)
    public JsonResult updateById(@RequestBody @Validated SysMenu sysMenu) {
        sysMenuService.editById(sysMenu);
        return JsonResult.success();
    }

    @DeleteMapping("/delete-by-id/{id}")
    @Operation(summary = "删除")
    @PreAuthorize("hasAnyAuthority('SYS_MENU_DEL')")
    @SysLog(msg = "删除菜单", type = DicConstant.OperLogType.DEL)
    public JsonResult delete(@PathVariable(value = "id") String id) {
        sysMenuService.delById(id);
        return JsonResult.success();
    }

}
